﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Trade.Model;

namespace Trade.DAL
{
    public class Sales
    {
        //创建临时数据表存放当前的SalesOrder的明细资料行
        public static DataTable SalesDetailTemplate()
        {
            string cmdString = "Select * from SalesDetail where 1 > 2";

            using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.connectionString, CommandType.Text, cmdString, null))
            {
                return Utility.ConvertToDataTable(rdr);
            }
        }

        //生成新的销售单编号
        public static string GenInvoiceNo(string curDate)
        {
            string cmdString = "exec Usp_SerialNo @p_SerialNoName, @p_FieldName, @p_TableName, @p_PrefixTime, @p_RSerialNo out";
            string salesNo = "";

            SqlParameter[] parms = {
                new SqlParameter("@p_SerialNoName", SqlDbType.VarChar, 30),
                new SqlParameter("@p_FieldName", SqlDbType.VarChar, 30),
                new SqlParameter("@p_TableName", SqlDbType.VarChar, 30),
                new SqlParameter("@p_PrefixTime", SqlDbType.VarChar, 8),
                new SqlParameter("@p_RSerialNo", SqlDbType.VarChar, 30)};

            parms[0].Value = "SerialNo";
            parms[1].Value = "SalesNo";
            parms[2].Value = "SalesMaster";
            parms[3].Value = curDate.Trim();
            parms[4].Value = salesNo;
            parms[4].Direction = ParameterDirection.InputOutput;

            SQLHelper.ExecuteNonQuery(SQLHelper.connectionString, CommandType.Text, cmdString, parms);
            salesNo = parms[4].Value.ToString();

            return salesNo;
        }

        public void Modify(SalesMasterInfo salesMaster, DataTable dtSalesDetail)
        {

        }

        private static SqlParameter[] SetSalesMasterParameters(SalesMasterInfo salesMaster, string userId)
        {
            SqlParameter[] parms = {
                new SqlParameter("@SalesNo", SqlDbType.VarChar, 30),
                new SqlParameter("@CustomerNo", SqlDbType.VarChar, 8), 
                new SqlParameter("@Dept", SqlDbType.Int),
                new SqlParameter("@ContractName", SqlDbType.VarChar, 30),
                new SqlParameter("@ContractNo", SqlDbType.VarChar, 30), 
                new SqlParameter("@Warehouse", SqlDbType.Int), 
                new SqlParameter("@Employee", SqlDbType.VarChar, 20), 
                new SqlParameter("@ReceiveAccount", SqlDbType.VarChar, 30), 
                new SqlParameter("@ReceiveAmount", SqlDbType.Decimal, 15),
                new SqlParameter("@ReceiveDate", SqlDbType.DateTime), 
                new SqlParameter("@SalesDate", SqlDbType.DateTime), 
                new SqlParameter("@EntryDate", SqlDbType.DateTime), 
                new SqlParameter("@EntryEmployee", SqlDbType.VarChar, 20),
                new SqlParameter("@InvoiceNo", SqlDbType.VarChar, 30),
                new SqlParameter("@InvoiceDate", SqlDbType.DateTime),
                new SqlParameter("@ShipFormNo", SqlDbType.VarChar, 30),
                new SqlParameter("@ShipDate", SqlDbType.DateTime),
                new SqlParameter("@ReferenceNo", SqlDbType.VarChar, 30), 
                new SqlParameter("@Remark", SqlDbType.VarChar, 120),
                new SqlParameter("@RecordId", SqlDbType.Int),
                new SqlParameter("@UserId", SqlDbType.VarChar, 15), 
                new SqlParameter("@ErrMsg", SqlDbType.VarChar, 100),
                new SqlParameter("@RFlag", SqlDbType.SmallInt)};

            parms[0].Value = salesMaster.SalesNo.Trim();
            parms[1].Value = salesMaster.CustomerNo.Trim();
            parms[2].Value = salesMaster.Dept;
            parms[3].Value = salesMaster.ContractName.Trim();
            parms[4].Value = salesMaster.ContractNo.Trim();
            parms[5].Value = salesMaster.Warehouse;
            parms[6].Value = salesMaster.Employee.Trim();
            parms[7].Value = salesMaster.ReceiveAccount.Trim();
            parms[8].Value = salesMaster.ReceiveAmount;
            parms[9].Value = salesMaster.ReceiveDate;
            parms[10].Value = salesMaster.SalesDate;
            parms[11].Value = salesMaster.EntryDate;
            parms[12].Value = salesMaster.EntryEmployee.Trim();
            parms[13].Value = salesMaster.InvoiceNo.Trim();
            parms[14].Value = salesMaster.InvoiceDate;
            parms[15].Value = salesMaster.ShipFormNo.Trim();
            parms[16].Value = salesMaster.ShipDate;
            parms[17].Value = salesMaster.ReferenceNo;
            parms[18].Value = salesMaster.Remark.Trim();
            parms[19].Value = salesMaster.RecordId;
            parms[19].Direction = ParameterDirection.InputOutput;
            parms[20].Value = userId.Trim();
            parms[21].Value = "";
            parms[21].Direction = ParameterDirection.InputOutput;
            parms[22].Value = 0;
            parms[22].Direction = ParameterDirection.InputOutput;

            return parms;
        }

        private static SqlParameter[] SetSalesDetailParameters(SalesDetailInfo salesDetail, string userId)
        {
            SqlParameter[] parms = {
                new SqlParameter("@SalesNo", SqlDbType.VarChar, 30),
                new SqlParameter("@LineNo", SqlDbType.VarChar, 3), 
                new SqlParameter("@ProductNo", SqlDbType.VarChar, 10),
                new SqlParameter("@Qty", SqlDbType.Decimal, 10), 
                new SqlParameter("@Price", SqlDbType.Decimal, 10), 
                new SqlParameter("@Amount", SqlDbType.Decimal, 15), 
                new SqlParameter("@DiscountRate", SqlDbType.Decimal, 15),
                new SqlParameter("@DiscountPrice", SqlDbType.Decimal, 10), 
                new SqlParameter("@DiscountAmt", SqlDbType.Decimal, 15), 
                new SqlParameter("@TaxRate", SqlDbType.Decimal, 10), 
                new SqlParameter("@TaxAmount", SqlDbType.Decimal, 15),
                new SqlParameter("@PriceInTax", SqlDbType.Decimal, 10),
                new SqlParameter("@AmountInTax", SqlDbType.Decimal, 15),
                new SqlParameter("@Remark", SqlDbType.VarChar, 60),
                new SqlParameter("@RecordId", SqlDbType.Int),
                new SqlParameter("@UserId", SqlDbType.VarChar, 15), 
                new SqlParameter("@ErrMsg", SqlDbType.VarChar, 100),
                new SqlParameter("@RFlag", SqlDbType.SmallInt)};

            parms[0].Value = salesDetail.SalesNo.Trim();
            parms[1].Value = salesDetail.LineNo.Trim();
            parms[2].Value = salesDetail.ProductNo.Trim();
            parms[3].Value = salesDetail.Qty;
            parms[4].Value = salesDetail.Price;
            parms[5].Value = salesDetail.Amount;
            parms[6].Value = salesDetail.DiscountRate;
            parms[7].Value = salesDetail.DiscountPrice;
            parms[8].Value = salesDetail.DiscountAmt;
            parms[9].Value = salesDetail.TaxRate;
            parms[10].Value = salesDetail.TaxAmount;
            parms[11].Value = salesDetail.PriceInTax;
            parms[12].Value = salesDetail.AmountInTax;
            parms[13].Value = salesDetail.Remark.Trim();
            parms[14].Value = salesDetail.RecordId;
            parms[14].Direction = ParameterDirection.InputOutput;
            parms[15].Value = userId.Trim();
            parms[16].Value = "";
            parms[16].Direction = ParameterDirection.InputOutput;
            parms[17].Value = 0;
            parms[17].Direction = ParameterDirection.InputOutput;

            return parms;
        }
    }
}
